package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/upload")
public class UploadController {
    @Value("${file.dir}")
    private String dir;
    @Reference
    private IUserInfoService userInfoService;

    @RequiredLogin
    @RequestMapping("/file")
    @ResponseBody
    public Object file(MultipartFile files, @LoginUser UserInfo user) throws Exception {
        AjaxResult result = new AjaxResult();
        String upload = UploadUtil.upload(files, dir);
        upload="/"+upload;
        userInfoService.updateHeadImgUrl(upload,user.getId());
        result.addData(upload);
        return result;
    }
}